/* ****版权所有
 */
package com.wxy.bank.dao;

import com.wxy.bank.dao.AccountDao;
import com.wxy.bank.model.Account;
import com.wxy.bank.util.DBUtil;
import com.wxy.bank.util.StringEncodeUTF8;

import java.awt.print.PrinterAbortException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author wxy
 * @version 1.0
 */
public class AccountDaoImpl implements AccountDao {

    /**
     * 添加一个新的账户
     * @param account 账户信息
     * @return 如果添加成功，返回非整数
     */
    @Override
    public int insertAccount(Account account) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int flag = 0;
        conn = DBUtil.getConnection();
        String sql = "insert into account Values(?,?,?,?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            //UUID 常用他做主键
            pstmt.setString(1, UUID.randomUUID().toString());
            pstmt.setString(2, account.getAccountName());
            pstmt.setString(3, account.getAccountPsw());
            pstmt.setString(4, account.getAccountNo());
            pstmt.setString(5, account.getBank());
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(null,pstmt,conn);
        }

        return flag;
    }

    @Override
    public List<Account> selectAllAccount() {
        List<Account> acclist = new ArrayList<Account>();
        Connection conn=null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Account account =null;

        conn = DBUtil.getConnection();
        String sql = "select id,accountname,accountpsw,accountno,bank from account";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()){
                account = new Account();
                account.setId(rs.getString("id"));
                account.setAccountName(rs.getString("accountname"));
                account.setAccountPsw(rs.getString("accountpsw"));
                account.setAccountNo(rs.getString("accountno"));
                account.setBank(rs.getString("bank"));
                acclist.add(account);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(rs,pstmt,conn);
        }
        return acclist;
    }

    @Override
    public int deleteAccount(Account account) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        conn = DBUtil.getConnection();
        int flag = 0;
        String sql = "delete from account where id=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,"id");
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(null,pstmt,conn);
        }


        return flag;
    }
}
